package com.heima.wemedia.controller.v1;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.model.admin.dtos.SensitiveDto;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.wemedia.pojos.WmSensitive;
import com.heima.wemedia.service.ISensitiveService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RequiredArgsConstructor
@RestController
@Slf4j
public class WmSensitiveController {

    private final ISensitiveService sensitiveService;

    @PostMapping("/api/v1/sensitive/list")
    public PageResponseResult list(@RequestBody String str){
        SensitiveDto dto = JSON.parseObject(str, SensitiveDto.class);
        log.info("敏感词分页查询:{}",dto);
        return sensitiveService.pageList(dto);
    }

    @PostMapping("/api/v1/sensitive/del")
    public ResponseResult deleteById(@RequestBody Integer id){
        log.info("删除敏感词:{}",id);
        Boolean result = sensitiveService.removeById(id);
        return ResponseResult.okResult(result);
    }

    @PostMapping("/api/v1/sensitive/save")
    @CacheEvict(cacheNames = "sensitiveCache",allEntries = true)
    public ResponseResult save(@RequestBody WmSensitive wmSensitive){
        log.info("新增敏感词");
        sensitiveService.save(wmSensitive);
        return ResponseResult.okResult(null);
    }

    @PostMapping("/api/v1/sensitive/update")
    public ResponseResult update(@RequestBody WmSensitive wmSensitive){
        log.info("修改敏感词:{}",wmSensitive);
        sensitiveService.updateById(wmSensitive);
        return ResponseResult.okResult(null);
    }

    @PostMapping("/api/v1/sensitive/excelImport")
    public ResponseResult excelImport(MultipartFile file) {
        sensitiveService.excelImport(file);
        return ResponseResult.okResult(null);
    }

    @GetMapping("/api/v1/sensitive/excelExport")
    public ResponseResult excelExport(String filePath) {
        sensitiveService.excelExport(filePath);
        return ResponseResult.okResult(null);
    }
}
